<?php

class reimburse {

	function __construct(){
		global $tpl;
		$this->tpl = $tpl;
		$user = authenticate();
		if(isset($user['user']) && $user['user_id']==1){
			$tpl->assign('user',$user);
		}else{
			redirect(BASE_URL);
		}
	}

	public function view_assignreimburse(){
		//查询需要报销的任务
		//查找功能和分页功能是一起的

		$a_title = (isset($_GET['a_title']) && !empty($_GET['a_title'])) ? $_GET['a_title'] : '';
		$c_id = (isset($_GET['c_id']) && !empty($_GET['c_id'])) ? intval($_GET['c_id']) : 0;
		$cs_id = (isset($_GET['cs_id']) && !empty($_GET['cs_id'])) ? intval($_GET['cs_id']) : 0;
		$a_sdate = (isset($_GET['a_sdate']) && !empty($_GET['a_sdate'])) ? $_GET['a_sdate'] : '';
		$a_edate = (isset($_GET['a_edate']) && !empty($_GET['a_edate'])) ? $_GET['a_edate'] : '';

		$con['a_title'] = $a_title;
		$con['c_id'] = $c_id;
		$con['cs_id'] = $cs_id;
		$con['a_sdate'] = $a_sdate;
		$con['a_edate'] = $a_edate;
		$con['a_auditbill'] = 1;
		$con['need_express'] = 0;
		$con['a_isreim'] = 0;

		$corps = array();

		include_once 'ReimburseModel.class.php';
		$reimbursemod = new ReimburseModel;

		$needreim = $reimbursemod->getItems($con,'assignment',10);

//		$needreim = $reimbursemod->getReimAssign();
		$total = $needreim['page']->total;

//		echo "<pre/>";
//		print_r($needreim);


		//需要的信息：品牌名称 任务标题 神秘顾客姓名 汇款账号 原定报销金额 实际餐费 是否通过发票审批 待报销的餐费
		//已经筛选出来的信息：任务标题、是否通过发票审批
		if(is_array($needreim['records']) && !empty($needreim['records'])){
			foreach($needreim['records'] as $k=>$v){
				//待筛选的信息：
				// 1、品牌名称：通过b_id在brand中来查找
				$bid = $v['b_id'];
				$brandtitle = $reimbursemod->getBrandTitle($bid);

				// 2、神秘顾客姓名 汇款账号：通过user_id在user_ext表中查找
				$user_id = $v['user_id'];
				$userpay = $reimbursemod->getUserPay($user_id);

				// 3、原定报销金额：通过cs_id 在store表中查找
				$cs_id = $v['cs_id'];
				$storereim = $reimbursemod->getStoreReim($cs_id);

				// 4、实际餐费：$v['a_cost']的值

				// 5、待报销餐费：取原定报销金额和实际餐费之中较小值
				$reim = $v['a_cost'] < $storereim['cs_reim'] ? $v['a_cost']:$storereim['cs_reim'];

				// 6、将查询到的信息添加到数组中，以便在显示时能够直接读取
				$needreim['records'][$k]['b_name'] = $brandtitle['b_name'];
				$needreim['records'][$k]['user_realname'] = $userpay['realname'];
				$needreim['records'][$k]['user_bankrealname'] = $userpay['bank_realname'];
				$needreim['records'][$k]['user_bankaccount'] = $userpay['bank_account'];
				$needreim['records'][$k]['user_alipay'] = $userpay['alipay'];
				$needreim['records'][$k]['user_alipayrealname'] = $userpay['alipayrealname'];
				$needreim['records'][$k]['storereim'] = $storereim['cs_reim'];
				$needreim['records'][$k]['shouldreim'] = $reim;
			}
		}
//		echo "<pre/>";
//		print_r($needreim);

		$this->tpl->assign('needreim',$needreim['records']);
		$this->tpl->assign('pageBar',$needreim['pagebar']);
		$this->tpl->assign('type','noback');
		$this->tpl->assign('total',$total);
		$this->tpl->assign('con',$con);
		$this->tpl->assign('corps',$corps);
	}

	public function view_expressreimburse(){
		include_once 'ReimburseModel.class.php';
		$reimbursemod = new ReimburseModel;

		$a_title = (isset($_GET['a_title']) && !empty($_GET['a_title'])) ? $_GET['a_title'] : '';
		$app_realname = (isset($_GET['app_realname']) && !empty($_GET['app_realname'])) ? $_GET['app_realname'] : '';

		$con = array();
		$con['a_title'] = $a_title;
		$con['app_realname'] = $app_realname;
		$con['recv_extend'] = 0;
		$con['is_received'] = 1;
		$con['exp_isreim'] = 0;
		$needreim = $reimbursemod->getItems($con, 'express',10);
		$total = $needreim['page']->total;

		if(is_array($needreim['records']) && !empty($needreim['records'])){
		//需要的信息：品牌、任务标题、姓名、汇款账号、补汇金额、小票已到
			foreach($needreim['records'] as $k=>$v){
				//品牌信息：通过a_id在assignment表中找到b_id,再通过b_id在brand表中找到品牌名称
				$a_id = $v['a_id'];
				$b_id = $reimbursemod->getBrandId($a_id);
				$brandtitle = $reimbursemod->getBrandTitle($b_id);
				//发票是否已通过审批：通过a_id在assignment中查找
				$isauditbill = $reimbursemod->getAssignInfo($a_id);

				//姓名、汇款账号：通过user_id在user_ext表中查找
				$user_id = $v['user_id'];
				$userpay = $reimbursemod->getUserPay($user_id);

				//将查询到的信息，添加到数组中
				$needreim['records'][$k]['b_title'] = $brandtitle['b_name'];
				$needreim['records'][$k]['b_id'] = $b_id;
				$needreim['records'][$k]['realname'] = $userpay['realname'];
				$needreim['records'][$k]['bank_realname'] = $userpay['bank_realname'];
				$needreim['records'][$k]['bank_account'] = $userpay['bank_account'];
				$needreim['records'][$k]['alipayrealname'] = $userpay['alipayrealname'];
				$needreim['records'][$k]['alipay'] = $userpay['alipay'];
				$needreim['records'][$k]['a_auditbill'] = $isauditbill['a_auditbill'];
			}
		}

//		echo "<pre/>";
//		print_r($needreim);

		$this->tpl->assign('needreim',$needreim['records']);
		$this->tpl->assign('total',$total);
		$this->tpl->assign('type','needback');
		$this->tpl->assign('pagebar',$needreim['pagebar']);
		$this->tpl->assign('con',$con);
	}

	public function view_showreimburse(){
		//不需要寄回快递的报销页面

		include_once 'ReimburseModel.class.php';

		$a_id = $_GET['a_id'];
		$b_id = $_GET['b_id'];
		$cs_id = $_GET['cs_id'];
		$user_id = $_GET['user_id'];

		$reimmod = new ReimburseModel;

		//需得到的信息：
		//1、任务所对应的品牌名称
		$b_name = $reimmod -> getBrandTitle($b_id);
		//2、任务标题、实际餐费
		$a_info = $reimmod -> getAssignTitle($a_id);
		//3、神秘顾客姓名、汇款账户及户名
		$userpay = $reimmod->getUserPay($user_id);
		//4、原定报销金额
		$storereim = $reimmod->getStoreReim($cs_id);
		//5、应该报销金额
		$reim = $a_info['a_cost'] < $storereim['cs_reim'] ? $a_info['a_cost']:$storereim['cs_reim'];

		$this->tpl->assign("b_name",$b_name['b_name']);
		$this->tpl->assign("a_info",$a_info);
		$this->tpl->assign("userpay",$userpay);
		$this->tpl->assign("storereim",$storereim['cs_reim']);
		$this->tpl->assign("reim",$reim);
		$this->tpl->assign("a_id",$a_id);
	}

	public function op_savereimburse(){
		//获得报销信息，并存入reimbursement表中
		//获取基本信息
		$a_id = (isset($_POST['a_id']) && !empty($_POST['a_id'])) ?  intval($_POST['a_id']) : 0;
		$brand_name = (isset($_POST['brand_name']) && !empty($_POST['brand_name'])) ? $_POST['brand_name'] : '';
		$a_title = (isset($_POST['a_title']) && !empty($_POST['a_title'])) ? $_POST['a_title'] : '';
		$user_realname = (isset($_POST['user_realname']) && !empty($_POST['user_realname'])) ? $_POST['user_realname'] : '';
		$user_account = (isset($_POST['user_account']) && !empty($_POST['user_account'])) ? $_POST['user_account'] : '';
		$store_reim = (isset($_POST['store_reim']) && !empty($_POST['store_reim'])) ? intval($_POST['store_reim']) : 0;
		$meal_cost = (isset($_POST['meal_cost']) && !empty($_POST['meal_cost'])) ? intval($_POST['meal_cost']) : 0;
		$shouldreim = (isset($_POST['shouldreim']) && !empty($_POST['shouldreim'])) ? intval($_POST['shouldreim']) : 0;
		$actuallyreim = (isset($_POST['acctuallyreim']) && !empty($_POST['acctuallyreim'])) ? intval($_POST['acctuallyreim']) : 0;
		$reim_date = (isset($_POST['reim_date']) && !empty($_POST['reim_date'])) ? $_POST['reim_date']: '';
		$reim_way = (isset($_POST['reim_way']) && !empty($_POST['reim_way'])) ? $_POST['reim_way'] : '';
		$is_express = (isset($_POST['is_express']) && !empty($_POST['is_express'])) ? intval($_POST['is_express']) : 0;
		$ex_id = (isset($_POST['ex_id']) && !empty($_POST['ex_id'])) ? intval($_POST['ex_id']) : 0;

		//1、将信息存入reimbursement表中
		include_once 'ReimburseModel.class.php';
		$reimmod = new ReimburseModel;

		$rs = $reimmod -> saveReimburse($a_id,$brand_name,$a_title,$user_realname,$user_account,$store_reim,$meal_cost,$shouldreim,$actuallyreim,$reim_date,$reim_way,$is_express);

		if($rs){
			if($is_express){
				//2、修改express表中该任务的exp_isreim的值为1，表示该任务已经报销
				$result = $reimmod -> updateExpAssign($ex_id);
			}else{
				//2、修改assignment表中该任务的a_isreim的值为1，表示该任务已经报销
				$result = $reimmod -> updateAssign($a_id);
			}

			if($result){
				show_message("success");
				redirect("admin.php/reimburse/assignreimburse");
				//show_message_goback("success");
			}
		}else{
			show_message("faild");
			redirect("admin.php/reimburse/assignreimburse");
		}
	}

	public function view_showreimburseexp(){
		//需寄回快递的报销页面

		//需要的信息：a_id 品牌名称 任务标题 神秘顾客姓名 汇款账号 应报销金额
		include_once 'ReimburseModel.class.php';

		$a_id = $_GET['a_id'];
		$b_id = $_GET['b_id'];
		$user_id = $_GET['user_id'];
		$ex_id = $_GET['ex_id'];

		$reimmod = new ReimburseModel;

		//需得到的信息：
		//1、任务所对应的品牌名称
		$b_name = $reimmod -> getBrandTitle($b_id);
		//2、任务标题、实际餐费
		$a_info = $reimmod -> getAssignTitle($a_id);
		//3、神秘顾客姓名、汇款账户及户名
		$userpay = $reimmod->getUserPay($user_id);
		//5、应该报销金额
		$reim = $_GET['reim'];

		$this->tpl->assign("b_name",$b_name['b_name']);
		$this->tpl->assign("a_info",$a_info);
		$this->tpl->assign("userpay",$userpay);
		$this->tpl->assign("reim",$reim);
		$this->tpl->assign("a_id",$a_id);
		$this->tpl->assign("ex_id",$ex_id);
	}

	public function view_havereimburse(){

		$con = array();
		$a_title = (isset($_GET['a_title']) && !empty($_GET['a_title'])) ? $_GET['a_title'] : '';
		$b_name = (isset($_GET['b_name']) && !empty($_GET['b_name'])) ? $_GET['b_name'] : '';
		$user_realname = (isset($_GET['user_realname']) && !empty($_GET['user_realname'])) ? $_GET['user_realname'] : '';

		$con['a_title'] = $a_title;
		$con['b_name'] = $b_name;
		$con['user_realname'] = $user_realname;

		include_once 'ReimburseModel.class.php';
		$reimmod = new ReimburseModel;

		$reims = $reimmod->getItems($con,'reimbursement',10);

		$this->tpl->assign("reims",$reims['records']);
		$this->tpl->assign("total",$reims['page']->total);
		$this->tpl->assign("pagebar",$reims['pagebar']);
		$this->tpl->assign("con",$con);
	}

	public function view_toexcel(){
		//查询符合条件的任务
		$con = array();

		include_once 'ReimburseModel.class.php';
		$reimmod = new ReimburseModel;

		$reim = $reimmod->getItemsWithNopage($con,'reimbursement',10);

		//导出为excel

		//导出为本地excel
		/** 导出为EXCEL start **/

		/** Error reporting */
		error_reporting(E_ALL);

		//date_default_timezone_set('Europe/London');

		/** PHPExcel */
		require_once 'plugins/phpexcel/Classes/PHPExcel.php';


		// Create new PHPExcel object
		$objPHPExcel = new PHPExcel();

		// Set properties
		$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
									 ->setLastModifiedBy("Maarten Balliauw")
									 ->setTitle("Office 2007 XLSX Test Document")
									 ->setSubject("Office 2007 XLSX Test Document")
									 ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
									 ->setKeywords("office 2007 openxml php")
									 ->setCategory("Test result file");

		//设置标题
		$objPHPExcel->setActiveSheetIndex(0)
		            ->setCellValue('A1', '已报销汇总')
		            ->setCellValue('A2', '品牌名称')
		            ->setCellValue('B2', '任务名称')
		            ->setCellValue('C2', '姓名')
		            ->setCellValue('D2', '汇款账号')
		            ->setCellValue('E2', '原定报销金额')
		            ->setCellValue('F2', '实际餐费')
		            ->setCellValue('G2', '实际转账金额')
		            ->setCellValue('H2', '是否已报销')
		            ->setCellValue('I2', '报销日期')
		            ->setCellValue('J2', '报销方式');


		//设置表格内容

		if(is_array($reim) && !empty($reim)){
			for($i=0,$j=3;$i<count($reim); $i++){
				$v = $reim[$i];

				$objPHPExcel->setActiveSheetIndex(0)
							->setCellValue('A'.$j, $v['b_name'])
							->setCellValue('B'.$j, $v['a_title'])
							->setCellValue('C'.$j, $v['user_realname'])
							->setCellValue('D'.$j, $v['user_account'])
							->setCellValue('E'.$j, $v['store_reim'])
							->setCellValue('F'.$j, $v['meal_cost'])
							->setCellValue('G'.$j, $v['reim_actually'])
							->setCellValue('H'.$j, $v['is_reim'])
							->setCellValue('I'.$j, $v['reim_date'])
							->setCellValue('J'.$j, $v['reim_way']);
				$j++;
			}
		}
		$objPHPExcel->setActiveSheetIndex(0);

		// Rename sheet
		$objPHPExcel->getActiveSheet()->setTitle('a_title');


		// Set active sheet index to the first sheet, so Excel opens this as the first sheet
		$objPHPExcel->setActiveSheetIndex(0);


		// Redirect output to a client’s web browser (Excel2007)
		header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
		header('Content-Disposition: attachment;filename="applicant.xlsx"');
		header('Cache-Control: max-age=0');

		$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
		$objWriter->save('php://output');
		exit;
		/** 导出为EXCEL end **/


	}

}
?>